package com.wangmengtao.upms.service.impl;


import java.util.List;

import com.wangmengtao.upms.dao.UpmsDeptUserDao;
import com.wangmengtao.upms.dao.UpmsUserDao;
import com.wangmengtao.upms.dao.UpmsUserDetailDao;
import com.wangmengtao.upms.dao.UpmsUserPasswordDao;


import com.wangmengtao.upms.model.UpmsUser;
import com.wangmengtao.upms.service.UpmsUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName: UpmsUserServiceImpl
 * @Description: TODO
 * @Author: WangMengtao
 * @Data: 2019/6/5 17:15
 * @Version: 1.0.0
 */
@Service
public class UpmsUserServiceImpl implements UpmsUserService {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UpmsUserDao upmsUserDao;
    @Autowired
    private UpmsUserPasswordDao upmsUserPasswordDao;
    @Autowired
    private UpmsUserDetailDao upmsUserDetailDao;
    @Autowired
    private UpmsDeptUserDao upmsDeptUserDao;

    @Override
    public int insert(UpmsUser upmsUser) {
        if (upmsUserDao.getByPhone(upmsUser.getPhone()) != null){
            return 0;
        }
        return upmsUserDao.insert(upmsUser);
    }

    @Override
    public int deleteById(Long id) {
        if (upmsUserDao.getById(id) != null){
            upmsUserDao.deleteById(id);
            upmsUserPasswordDao.deleteById(id);
            upmsUserDetailDao.deleteById(id);
            upmsDeptUserDao.deleteByUserId(id);
            return 1;
        }
        return 0;
    }

    @Override
    public int update(UpmsUser upmsUser) {

        return upmsUserDao.update(upmsUser);
    }

    @Override
    public int deleteByIds(List<Long> ids) {
        return 0;
    }

    @Override
    public UpmsUser getById(Long id) {
        return upmsUserDao.getById(id);
    }

    @Override
    public List<UpmsUser> list() {
        return upmsUserDao.list();
    }

    @Override
    public List<UpmsUser> listByDeptId(Long deptId) {
        // TODO 自动生成的方法存根
        return upmsUserDao.listByDeptId(deptId);
    }

    @Override
    public UpmsUser getByPhone(String phone, String password) throws Exception {
        UpmsUser upmsUser = upmsUserDao.getByPhone(phone);
        logger.info("password:" + password);
        logger.info("id:" + upmsUser.getId());
        logger.info("upmsUserPasswordDao.getById(upmsUser.getId()).getPassword() :"+ upmsUserPasswordDao.getById(upmsUser.getId()).getPassword());
        if (password == upmsUserPasswordDao.getById(upmsUser.getId()).getPassword()) {
            return upmsUser;
        }
        throw new Exception();
    }

    @Override
    public UpmsUser getByEmail(String email, String password) throws Exception {
        UpmsUser upmsUser = upmsUserDao.getByEmail(email);
        if (password.equals(upmsUserPasswordDao.getById(upmsUser.getId()))) {
            return upmsUser;
        }
        throw new Exception();
    }

}
